1. Technical Field
The present invention is directed to an apparatus and method for avoiding deadlocks in a multithreaded environment.
2. Description of Related Art
Java Just-In-Time (JIT) compilers compile Java bytecode to native instructions of a target processor. This can occur as late as when a method is first executed. Java is a multithreaded language and thus, it is possible that two or more threads of execution could simultaneously attempt to execute the same method for the first time in the system. If the method was compiled by both threads then time and space would be wasted. A lock may be used to prevent one thread from compiling a method which has already started to be complied by another thread. However, improper use of locks may lead to a deadlock situation.
For example, when a method is compiled, the JIT compiler may need to resolve some constants which involve calling back into the Java Virtual Machine (JVM). It is possible that more Java code will be executed in the process of resolving the constant, particularly if a class loader is involved. If the process of constant resolution calls the same method that is being compiled then a problem arises because the half-compiled method cannot be executed. Prior art solutions to this deadlock situation typically involve embeddeding lock acquisition calls in the compiled code and removing them as compilation progresses.
Thus, it would be beneficial to have an apparatus and method for avoiding simultaneous compilation of the same method by two different execution threads. It would further be beneficial to have an apparatus and method for avoiding simultaneous compilation while also avoiding deadlocks in a multithreaded environment.